package integral;

import enums.ActivityStatus;
import models.BaseModel;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import models.merchant.Merchant;

import javax.persistence.*;
import java.util.Date;

/**
 *积分活动
 * Created by youliangcheng on 17/11/18.
 */
@Entity
@Table(name = "integral_activitys")
public class IntegralActivity  extends BaseModel{

    /**
     * 编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 标题
     */
    @Column(name = "title")
    public String title;

    @ManyToOne
    @JoinColumn(name = "merchant_id")
    public Merchant merchant;

    /**
     * 活动状态
     */
    @Column(name = "activity_status")
    @Enumerated(EnumType.STRING)
    public ActivityStatus activityStatus ;


    /**
     * 活动开启人
     */
    @ManyToOne
    @JoinColumn(name = "start_user_id")
    public AdminUser startUser;

    /**
     * 开启时间
     */
    @Column(name = "start_at")
    @Temporal(TemporalType.TIMESTAMP)
    public Date startAt;


    /**
     * 活动结束人
     */
    @ManyToOne
    @JoinColumn(name = "end_user_id")
    public AdminUser endUser;

    /**
     * 结束时间
     */
    @Column(name = "end_at")
    @Temporal(TemporalType.TIMESTAMP)
    public Date endAt;


    /**
     * 说明
     */
    @Column(name = "content")
    public String content;


    /**
     * 逻辑删除状态
     */
    @Column(name = "deleted")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 版本号
     */
    @Version
    public Integer version = 0;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;


    /**
     *
     * @param id
     * @return
     */
    public static IntegralActivity findByActivityId(long id){
        return IntegralActivity.find("id = ? and deleted = ?" , id , DeletedStatus.UN_DELETED).first();
    }

    /**
     * 查询商户是否已经有已启动的或者正在进行的活动
     * @param merchantId
     * @param status
     * @return
     */
    public static IntegralActivity findByMerchantAndActivityStatus(long merchantId , ActivityStatus status){
        return  IntegralActivity.find("deleted = ? and merchant.id = ? and activityStatus = ? ", DeletedStatus.UN_DELETED , merchantId , status).first();
    }




}
